package com.example.shardingsphere.demo.client;

import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author Sunny
 * @date 2021/1/23
 */
public class OrderClient {

    public static String createOrderNo(Long userId) {
        return ThreadLocalRandom.current()
                .nextInt(1000, 9999)
                + LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)
                + StringUtils.rightPad(userId.toString(), 4, '0');
    }

    public static int getTableIndex(String orderNo) {
        String userId = orderNo.substring(orderNo.length() - 4);
        return Integer.parseInt(userId) % 3;
    }

    public static boolean isTodayOrder(String orderNo) {
        String date = orderNo.substring(4, 12);
        return LocalDate.now().compareTo(LocalDate.parse(date, DateTimeFormatter.BASIC_ISO_DATE)) == 0;
    }

    public static int getTableIndex(Long userId) {
        return userId.intValue() % 3;
    }

    public static void main(String[] args) {
        Set<String> nos = new HashSet<>();

        for (int i = 0; i < 1000000; i++) {
            String orderNo = createOrderNo((long) ThreadLocalRandom.current()
                    .nextInt(1000, 9999));
            nos.add(orderNo);
        }

        System.out.println(nos.size());
        System.out.println(Arrays.toString(nos.stream().limit(1).toArray()));
    }
}
